<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Auth.php 3971 2010-07-25 10:26:42Z huuphuoc $
 * @since		2.0.0
 */

class Core_Services_Auth implements Zend_Auth_Adapter_Interface 
{
	/**
	 * Authenticated success
	 * Its value must be greater than 0
	 */
	const SUCCESS = 1;
	
	/**
	 * Constant define that user has not been active
	 * Its value must be smaller than 0
	 */
	const NOT_ACTIVE = -1;
	
	/**
	 * General failure
	 * Its value must be smaller than 0
	 */
	const FAILURE = -2;
	
	private $_username;
	private $_password;
	
	public function __construct($username, $password) 
	{
		$this->_username = $username;
		$this->_password = $password;
	}

	/**
     * Performs an authentication attempt
     *
     * @throws Zend_Auth_Adapter_Exception If authentication cannot be performed
     * @return Zend_Auth_Result
     */
    public function authenticate() 
    {    	
		$password = md5($this->_password);
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$dao = Tomato_Model_Dao_Factory::getInstance()->setModule('core')->getUserDao();
		$dao->setDbConnection($conn);
		
		$user = $dao->authenticate($this->_username, $password);
		if (null == $user) {
			return new Zend_Auth_Result(self::FAILURE, null);
		}
    	if (!$user->is_active) {
    		return new Zend_Auth_Result(self::NOT_ACTIVE, null);
    	}
		$employeeDao = Tomato_Model_Dao_Factory::getInstance()->setModule('employee')->getEmployeeDao();
		$employeeDao->setDbConnection($conn);
		
		$shopDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getShopDao();
		$shopDao->setDbConnection($conn);
		// lấy shopParent
		$shopParent  = $shopDao->getParentShop();
		if(!$shopParent){// chưa có shopParent thì tạo mới
			$shopParent   = new stdClass();
			$shopParent->id				= $shopDao->getNextId();
			$shopParent->shop_type 		= Tomato_Config::getConfig()->get("jam")->shop_type;
			$shopParent->shop_name_vi_VN	= Tomato_Config::getConfig()->get("jam")->shop_name_vi_VN;
			$shopParent->shop_name_en_US	= Tomato_Config::getConfig()->get("jam")->shop_name_en_US;
			$shopParent->adress				= Tomato_Config::getConfig()->get("jam")->shop_adress;
			$shopParent->tel				= Tomato_Config::getConfig()->get("jam")->shop_tel;
			$shopParent->fax				= Tomato_Config::getConfig()->get("jam")->shop_fax;
			$shopParent->currency			= Tomato_Config::getConfig()->get("jam")->shop_currency;
			$shopParent->parent_id			= NULL;
			$shopParent->tax_code			= Tomato_Config::getConfig()->get("jam")->shop_tax_code;
			$shopParent->introduction_vi_VN = Tomato_Config::getConfig()->get("jam")->shop_introduction_vi_VN;
			$shopParent->introduction_en_US = Tomato_Config::getConfig()->get("jam")->shop_introduction_en_US;
			$shopParent->rate				= 0;
			$shopDao->add($shopParent);
		}
    	$employee 	 = $employeeDao->getByCoreUserId($user->user_id);
    	// tìm trong employee thông tin nhân viên đó, nếu chưa có thì tạo mới nhân viên với thông tin mặc định được lấy từ application_...ini
    	if(!$employee){
    		$employee = new stdClass();
    		$employee->id				= $employeeDao->getNextId();
    		$employee->core_user_id		= $user->user_id;
    		$employee->shop_id			= $shopParent->id;
    		$employee->name				= Tomato_Config::getConfig()->get("jam")->employee_name;
    		$employee->sex				= Tomato_Config::getConfig()->get("jam")->employee_sex;
    		$employee->birthday			= Tomato_Config::getConfig()->get("jam")->employee_birthday;
    		$employee->identity_card_number = Tomato_Config::getConfig()->get("jam")->employee_identity_card_number;
    		$employee->description			= null;
    		$employeeDao->add($employee);	
    	}
    	// tìm chi tiết về shop mà nhân viên đó trực thuộc
    	$shop		= $shopDao->getById($employee->shop_id);
    	if(!$shop){// nếu chưa có thông tin về shop thì cũng tạo mặc định
			$shop   = new stdClass();
			$shop->id				= $employee->shop_id;
			$shop->shop_type 		= Tomato_Config::getConfig()->get("jam")->shop_type;
			$shop->shop_name_vi_VN	= Tomato_Config::getConfig()->get("jam")->shop_name_vi_VN;
			$shop->shop_name_en_US	= Tomato_Config::getConfig()->get("jam")->shop_name_en_US;
			$shop->adress			= Tomato_Config::getConfig()->get("jam")->shop_adress;
			$shop->tel				= Tomato_Config::getConfig()->get("jam")->shop_tel;
			$shop->fax				= Tomato_Config::getConfig()->get("jam")->shop_fax;
			$shop->currency			= Tomato_Config::getConfig()->get("jam")->shop_currency;
			$shop->parent_id		= NULL;
			$shop->tax_code			= Tomato_Config::getConfig()->get("jam")->shop_tax_code;
			$shop->introduction_vi_VN= Tomato_Config::getConfig()->get("jam")->shop_introduction_vi_VN;
			$shop->introduction_en_US= Tomato_Config::getConfig()->get("jam")->shop_introduction_en_US;
			$shop->rate				 = 0;
			$shopDao->add($shop);    		
    	}
    	$user->employee  = $employee;
    	$user->shop		 = $shop;
    	return new Zend_Auth_Result(self::SUCCESS, $user);
    }		
}
